package com.wise.service.cms.content.menu;

import com.wise.entities.content.Menu;
import com.wise.entities.content.MenuShow;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.session.GetAcceptLanguage;
import com.wise.summer.v1.cms.content.menu.AddMenu;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;

@Service
@SummerService
public class AddMenuService implements SummerServiceBean<AddMenu> {

    @Override
    public void sum(AddMenu model) {
        String lang = GetAcceptLanguage.s(true);
        List<Menu.ExtContent> extContentList = getAndCheckExtContent(model);

        Menu menu = new Menu();
        menu.setAlias(model.getAlias());
        menu.setParentId(model.getParentId());
        menu.setName(model.getName());
        menu.setIcon(model.getIcon());
        menu.setUrl(model.getUrl());
        menu.setSeqencing(model.getSeqencing());
        menu.setDescription(model.getDescription());
        menu.setTitlePic(model.getTitlePic());
        menu.setTitlePicHeight(model.getTitlePicHeight());
        if (model.getShowStatus() != null) {
            menu.setShowStatus(model.getShowStatus());
        } else {
            //如果传参为空，默认不显示
            menu.setShowStatus(MenuShow.NOTSHOW.value());
        }
        menu.setState(model.getState());
        menu.setLang(lang);
        menu.setCreatedBy(CurrentUserId.s(true));
        menu.setCreationDate(DateTimeUtil.timestampNow());
        menu.setType(model.getType());
        //check extContent防止只有新品推荐时数据库不存储其为列表的消息

        menu.setExtContent(model.getExtContent());
        menu.setNewRecommend(UpdateMenuService.getNewRecommendByExtContents(extContentList));
        DaoSave.s(menu);
        model.setSummerResult(menu);
    }

//    /**
//     * 防止只有新品推荐时数据库不存储其为列表的消息
//     *
//     * @param extContent
//     * @return
//     */
//    private String newProductRecommandHelper(String extContent) {
//        String onlyNewProductPattern = "[{\"type\":0}]";
//        if (!StringUtils.isNotBlank(extContent)) {
//            return extContent;
//        }
//        if (extContent.equals(onlyNewProductPattern)) {
//            return "[{\"type\":0},{\"type\":1}]";
//        }
//        return extContent;
//    }

    @SummerService(false)
    private List<Menu.ExtContent> getAndCheckExtContent(AddMenu model) {
        return UpdateMenuService.getAndCheckExtContent(model.getType(), model.getExtContent());
    }
}
